package com.pipi.biyesheji.util;

import lombok.Getter;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * 文件上传工具类
 * @author pipi
 */
@Getter
@Setter
public class UploadUtil {
    /*
        这里的字段image必须和上传页面upload.jsp中的image
        <input type="file" name="image" accept="image/*" />一样的名字
    */
    private MultipartFile image;

    public String uploadImage(MultipartFile image, HttpSession session) throws IOException {
        String imageName = image.getName() + ".jpg";
        File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);

        System.out.println(session.getServletContext().getRealPath("/images/product"));

        file.getParentFile().mkdirs();
        image.transferTo(file);


//            ProductVO vo = new ProductVO();
//            vo.setId(product.getId());
//            vo.setImageUrl("images/product/"+imageName);
//
//            productService.setImageURL(vo);
        return "images/product/" + imageName;
    }

}
